﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Linq;

namespace DienThoai
{
    public class Utility
    {
        public void ClearCombobox(ComboBox cbb)
        {
            int t = cbb.Items.Count;
            for (int i = t-1; i >= 0; i--)
            {
                cbb.Items.RemoveAt(i);
            }
        }
        DienThoaiDataContext db = new DienThoaiDataContext();
        public IQueryable getCustomerList()
        {
            return from i in db.Customers
                   select i;
        }
        public void getCustomerDataGrid(DataGrid grid)
        {
            var Customers = from i in db.Customers
                            select i;
            if (Customers.Count() != 0)
            {
                grid.ItemsSource = Customers;
            }
            else
                grid.ItemsSource = null;
        }
        public void getCustomerDataGrid(DataGrid grid, string key)
        {
            var Customers = from i in db.Customers
                            where i.Person.Fullname.Contains(key)
                            select i;
            if (Customers.Count() != 0)
            {
                grid.ItemsSource = Customers;
            }
            else
                grid.ItemsSource = null;
        }
        public void getCustomerDataGrid(DataGrid grid, int year)
        {
            var Customers = from i in db.Customers
                            where i.Date.Value.Year == year
                            select i;
            if (Customers.Count() != 0)
            {
                grid.ItemsSource = Customers;
            }
            else
                grid.ItemsSource = null;
        }
        public void getCustomerDataGrid(DataGrid grid, string key, int year)
        {
            var Customers = from i in db.Customers
                            where i.Person.Fullname.Contains(key) && i.Date.Value.Year == year
                            select i;
            if (Customers.Count() != 0)
            {
                grid.ItemsSource = Customers;
                grid.Columns.RemoveAt(5);
            }
            else
                grid.ItemsSource = null;
        }

        public void getStaffDataGrid(DataGrid grid)
        {
            var Staff = from i in db.Staffs
                        select i;

            if (Staff.Count() != 0)
            {
                grid.ItemsSource = Staff;
            }
            else
                grid.ItemsSource = null;
        }

        public void getStaffDataGrid(DataGrid grid, string role)
        {
            var Staff = from i in db.Staffs
                        where i.Role.RoleName == role
                        select i;
            if (Staff.Count() != 0)
            {
                grid.ItemsSource = Staff;
            }
            else
                grid.ItemsSource = null;
        }

        public void getStaffDataGrid(DataGrid grid, RadioButton Username, RadioButton Fullname, string key)
        {
            var Staff1 = from i in db.Staffs
                         where i.Person.Fullname.Contains(key)
                         select i;
            var Staff2 = from i in db.Staffs
                         where i.Username.Contains(key)
                         select i;
            if (Username.IsChecked == true)
            {
                if (Staff2.Count() != 0)
                {
                    grid.ItemsSource = Staff1;
                }
                else
                    grid.ItemsSource = null;
            }
            else
            {
                if (Staff1.Count() != 0)
                {
                    grid.ItemsSource = Staff1;
                }
                else
                    grid.ItemsSource = null;
            }
        }

        public void getStaffDataGrid(DataGrid grid, string role, RadioButton Username, RadioButton Fullname, string key)
        {
            var Staff1 = from i in db.Staffs
                         where i.Role.RoleName == role && i.Person.Fullname.Contains(key)
                         select i;
            var Staff2 = from i in db.Staffs
                         where i.Role.RoleName == role && i.Username.Contains(key)
                         select i;
            if (Username.IsChecked == true)
            {
                if (Staff2.Count() != 0)
                {
                    grid.ItemsSource = Staff1;
                }
                else
                    grid.ItemsSource = null;
            }
            else
            {
                if (Staff1.Count() != 0)
                {
                    grid.ItemsSource = Staff1;
                }
                else
                    grid.ItemsSource = null;
            }
        }
        //Bỏ role quản lý        
        public ComboBox getRole()
        {
            ComboBox b = new ComboBox();
            var role = from i in db.Roles
                       where i.RoleID != "0"
                       select i.RoleName;
            foreach (var i in role)
                b.Items.Add(i);
            return b;
        }

        //Form Bán hàng BillForm
        public DataGrid getProductDataGrid(DataGrid grid)
        {
            var query = from i in db.Products
                        select i;
            grid.ItemsSource = query;
            return grid;
        }

        /// <summary>
        /// This method is used to get phone data
        /// </summary>
        /// <param name="grid">Grid</param>
        /// <returns>Grid</returns>
        public DataGrid getPhoneDataGrid(DataGrid grid)
        {
            var query = from i in db.Products
                        from j in db.Phones
                        where i.ProductID == j.PhoneID
                        select i;
            grid.ItemsSource = query;
            return grid;
        }

        public DataGrid getPhoneDataGrid(DataGrid grid, string key)
        {
            var query = from i in db.Products
                        from j in db.Phones
                        where i.Model.Contains(key) && i.ProductID == j.PhoneID
                        select i;
            grid.ItemsSource = query;
            return grid;
        }

        public DataGrid getAccessoryDataGrid(DataGrid grid)
        {
            var query = from i in db.Products
                        from j in db.Accessories
                        where i.ProductID == j.AccessoryID
                        select i;
            grid.ItemsSource = query;
            return grid;
        }

        public DataGrid getAccessoryDataGrid(DataGrid grid, string key)
        {
            var query = from i in db.Products
                        from j in db.Accessories
                        where i.Model.Contains(key) && i.ProductID == j.AccessoryID
                        select i;
            grid.ItemsSource = query;
            return grid;
        }
        //Tab Quản lý bán hàng.
        public DataGrid Manager_getPhoneDataGrid(DataGrid grid)
        {
            var query = from i in db.Products
                        from j in db.Phones
                        where i.ProductID == j.PhoneID
                        select j;
            grid.ItemsSource = query;
            return grid;
        }

        public DataGrid Manager_getPhoneDataGrid(DataGrid grid, string key)
        {
            var query = from i in db.Products
                        from j in db.Phones
                        where i.Model.Contains(key) && i.ProductID == j.PhoneID
                        select j;
            grid.ItemsSource = query;
            return grid;
        }

        public DataGrid Manager_getAccessoryDataGrid(DataGrid grid)
        {
            var query = from i in db.Products
                        from j in db.Accessories
                        where i.ProductID == j.AccessoryID
                        select j;
            grid.ItemsSource = query;
            return grid;
        }

        public DataGrid Manager_getAccessoryDataGrid(DataGrid grid, string key)
        {
            var query = from i in db.Products
                        from j in db.Accessories
                        where i.Model.Contains(key) && i.ProductID == j.AccessoryID
                        select j;
            grid.ItemsSource = query;
            return grid;
        }
    }
}
